import tensorflow as tf
from tensorflow import keras
from sklearn.datasets import fetch_california_housing
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import numpy as np

def build_model(hidden_layers, layer_size, lr):
    model = keras.Sequential()
    model.add(keras.layers.Dense(layer_size, activation='relu'))  # 不要Input_shape
    for _ in range(hidden_layers-1):
        model.add(keras.layers.Dense(layer_size,activation='relu'))
    model.add(keras.layers.Dense(1))

    model.compile(loss='mse',optimizer = keras.optimizers.SGD(lr))

    return model 
    
# n_jobs 多核处理 要把运行代码写道主循环中　
# 避免不知道线程属于哪个程序 现在就知道线程都是属于main程序的
if __name__ == "__main__":
    
    dataset = fetch_california_housing()
    train_x, test_x, train_y, test_y = train_test_split(dataset.data,dataset.target,test_size=0.2,random_state=42)
    train_x, valid_x, train_y, valid_y = train_test_split(train_x,train_y,test_size=0.2,random_state=42)

    scaler = StandardScaler()
    train_x = scaler.fit_transform(train_x)
    valid_x = scaler.transform(valid_x)
    test_x = scaler.transform(test_x)

    model_sklearn = keras.wrappers.scikit_learn.KerasRegressor(build_model)

    import scipy
    params_distribution = {
        "hidden_layers" : scipy.stats.randint(1,4),
        "layer_size":scipy.stats.randint(1,100),
        # "lr" : scipy.stats.reciprocal(1e-4,1e-2) 
        "lr" : [0.0001,0.0003,0.001,0.003,0.01,0.03]
    }

    from sklearn.model_selection import RandomizedSearchCV


    model_sklearn_randomserch = RandomizedSearchCV(model_sklearn,params_distribution,n_iter=10,n_jobs=-1)
    model_sklearn_randomserch.fit(train_x,train_y,epochs=10)

    print(model_sklearn_randomserch.best_estimator_)  
    print(model_sklearn_randomserch.best_params_)  # 4 25 0.009 -- 3 58 0.01
    print(model_sklearn_randomserch.best_score_)  # -0.34 -- -0.34

    model = model_sklearn_randomserch.best_estimator_.model
    model.evaluate(test_x,test_y)  # 0.31  --  0.34

